package com.qc.bug.Controller;

import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.qc.bug.Services.bugInfoServices;
import com.qc.bug.Services.userInfoServices;
import com.qc.bug.Services.userSearchBugServices;
import com.qc.bug.Util.User;
import com.qc.bug.pojo.UserInfo;

@Controller
@RequestMapping(value="/userinfocontr")
public class userInfoController {
	
	@Autowired
	private userInfoServices userInfoBiz;
	@Autowired
	private userSearchBugServices uSearchBugBiz;
	@Autowired
	private bugInfoServices bugInfoBiz;
	
	
			/**
			 * 登录
			 * 
			 * @param phone
			 * @param pwd
			 * @param map
			 * @return 用户登录信息
			 */
	@RequestMapping(value="/userlogin")
	public String userLogin(@RequestParam String phone,@RequestParam String pwd,Map<String,Object> map,HttpSession session){
			
			UserInfo userinfo=userInfoBiz.userLogin(phone, pwd);
			if(null!=userinfo){
				session.setAttribute(User.USER_INFO, userinfo);
				userInfoBiz.updateLastLoginTime(userinfo);
			}
		
		return "home";
		
	}
	/**
	 * 注册用户
	 * @param userinfo
	 * @return
	 */
	@RequestMapping(value="/register")
	public String userRegister(UserInfo userinfo){
		
		int a=userInfoBiz.userRegister(userinfo);
		
		if(a>0){
			userInfoBiz.setRegistrTime(userinfo);
		}
		return "home";
		
	}
	/**
	 * 登录显示用户信息
	 * 根据用户id查询用户检索bug前五条信息
	 * 和用户所插入的bug信息前--分页显示
	 * @return
	 */
	@RequestMapping(value="/finduserinfo")
	public String findUserInfo(@RequestParam(defaultValue="1") int pageIndex,@RequestParam(defaultValue="1") int pageIndex2,@RequestParam(defaultValue="3") int pageSize,HttpSession session,Map<String,Object> map){
			UserInfo userInfo=(UserInfo) session.getAttribute(User.USER_INFO);
			/**
			 * 用户检索bug信息
			 */
			map.put("searchInfo", uSearchBugBiz.findUserSearchInfo(userInfo.getUserid(),pageIndex,pageSize));
			map.put("total", uSearchBugBiz.getSearchTotal(userInfo.getUserid()));
			map.put("pagenumber", uSearchBugBiz.getSearchNumber(uSearchBugBiz.getSearchTotal(userInfo.getUserid()),pageSize));
			map.put("pageIndex", pageIndex);
			/**
			 * 用户插入bug信息
			 */
			map.put("addBugInfo", bugInfoBiz.findByUserId(userInfo.getUserid(),pageIndex2,pageSize));
			map.put("pagenumber2", uSearchBugBiz.getSearchNumber(bugInfoBiz.getAddBugTotal(userInfo.getUserid()),pageSize));
			map.put("pageIndex2", pageIndex2);
			
			return "userindex";
		
	}
	
	@RequestMapping(value="/updatepwd")
	public String updatePwd(@RequestParam String phone,@RequestParam String password,@RequestParam String check,Map<String,Object> map,HttpSession session){
		String checkMsg=(String) session.getAttribute("checkMsg");
		if(check.equals(checkMsg)){
			//验证成功
			int i=userInfoBiz.updatePwd(phone,password);
			 if(i>0){
				 map.put("result", "密码修改成功！");
			 }else{
				 map.put("result","密码修改失败");
			 }
		}else{
			map.put("result", "请输入正确验证码！");
		}
		return "result/result";
		
	}
	
	@RequestMapping(value="/userout")
	public String userOut(HttpSession session){
		UserInfo userInfo=(UserInfo) session.getAttribute(User.USER_INFO);
		userInfo=null;
		session.setAttribute(User.USER_INFO, userInfo);
		
		return "home";
	}

}
